package com.lenovo.manufacture;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AirConditionerActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView coldImg;
    private ImageView hotImg;
    private Button coldBtn;
    private Button hotBtn;
    private ImageButton endBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air_conditioner);
        initView();


    }

    private void airCondinfo(int id,int k) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/Interface/UserFactory/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Network network = retrofit.create(Network.class);
        Call<AirConditionerData> airConditioner = network.getAirConditioner(id,k);
        airConditioner.enqueue(new Callback<AirConditionerData>() {
            @Override
            public void onResponse(Call<AirConditionerData> call, Response<AirConditionerData> response) {
                int acSwitch = response.body().getData().get(0).getAcSwitch();
                coldImg.setImageResource(R.drawable.xh);
                hotImg.setImageResource(R.drawable.ty);
                if (acSwitch==1){
                    coldImg.setImageResource(R.drawable.xh2);

                }else if(acSwitch==2) {
                    hotImg.setImageResource(R.drawable.ty2);
                }
            }

            @Override
            public void onFailure(Call<AirConditionerData> call, Throwable t) {

            }
        });

    }
    private void factoryinfo(int i,String z){
        Map<String,Object> map=new HashMap<>();
        map.put("id",1);
        map.put("acSwitch","0");
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/Interface/UserFactory/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Network network = retrofit.create(Network.class);
        Call<FactoryData> factoryInfo = network.getFactoryInfo(map);
        factoryInfo.enqueue(new Callback<FactoryData>() {
            @Override
            public void onResponse(Call<FactoryData> call, Response<FactoryData> response) {

                Log.d("TAG", "onResponse: "+response.body().getData().get(0).getIllumination()+response.body().getData().get(0).getGold());
//                if (acSwitch.equals(0)){
//                    coldImg.setImageResource(R.drawable.xh);
//                    hotImg.setImageResource(R.drawable.ty);
//                }else if (acSwitch.equals(1)){
//                    coldImg.setImageResource(R.drawable.xh2);
//                }else {
//                    hotImg.setImageResource(R.drawable.ty2);
//                }
            }

            @Override
            public void onFailure(Call<FactoryData> call, Throwable t) {

            }
        });
    }

    private void initView() {
        coldImg = (ImageView) findViewById(R.id.cold_img);
        hotImg = (ImageView) findViewById(R.id.hot_img);
        coldBtn = (Button) findViewById(R.id.cold_btn);
        hotBtn = (Button) findViewById(R.id.hot_btn);
        endBtn = (ImageButton) findViewById(R.id.end_btn);
        coldBtn.setOnClickListener(this::onClick);
        hotBtn.setOnClickListener(this::onClick);
        endBtn.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.end_btn:
                factoryinfo(1,"0");
                airCondinfo(1,0);

                break;
                case R.id.cold_btn:
                    factoryinfo(1,"1");
                    airCondinfo(1,1);
                break;
                case R.id.hot_btn:
                    factoryinfo(1,"2");
                    airCondinfo(1,2);
                break;
        }
    }
}